﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using BaseData.Common;
using BaseData.UI;
using TaxData.User;
using TaxEntity.User;

namespace TaxBiz.User
{
    /// <summary>
    /// 用户逻辑层
    /// </summary>
    public class UserBiz
    {
        private UserData ud = new UserData();

        /// <summary>
        /// 判断用户是否存在
        /// </summary>
        public bool Exists(string userName)
        {
            return ud.Exists(userName);
        }

        /// <summary>
        /// 获取用户信息实体(带缓存)
        /// </summary>
        public UserEntity GetEntityById(long userId)
        {
            string cacheName = "userdata_entity_" + userId;
            UserEntity model = Cache.GetData(cacheName) as UserEntity;
            if (model == null)
            {
                model = ud.GetEntityById(userId);
                Cache.Add(cacheName, model, DateTime.Now.AddHours(2));
            }
            return model;
        }

        /// <summary>
        /// 获取用户信息实体
        /// </summary>
        public UserEntity GetEntityByName(string userName)
        {
            UserEntity model = ud.GetEntityByName(userName); ;
            return model;
        }

        /// <summary>
        /// 增加用户
        /// </summary>
        /// <param name="model">用户信息实体</param>
        /// <param name="optUser">操作用户</param>
        /// <returns>成功用户编号,失败返回0</returns>
        public long Add(UserEntity model, UserEntity optUser)
        {
            long userID = ud.Add(model, optUser);
            if (userID > 0)
            {
                //记日志并清缓存
                model.UserId = userID;
                this.ClearEntityCache(userID);
            }
            return userID;
        }
        
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="model">修改用户</param>
        /// <param name="optUser">操作用户</param>
        /// <returns>操作结果</returns>
        public bool Update(UserEntity model, UserEntity optUser)
        {
            bool isSucc = ud.Update(model, optUser);
            if (isSucc)
            {
                //记日志并清缓存
                this.ClearEntityCache(model.UserId);
            }
            return isSucc;
        }

        /// <summary>
        /// 更新用户最后登录时间
        /// </summary>
        /// <param name="userID">用户编号</param>
        /// <returns>操作结果</returns>
        public bool UpdateLoginTime(long userID)
        {
            bool isSucc = ud.UpdateLoginTime(userID);
            if (isSucc)
            {
                //清缓存
                this.ClearEntityCache(userID);
            }
            return isSucc;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        public bool Delete(long userID, UserEntity optUser)
        {
            bool isSucc = ud.Delete(userID, optUser);
            if (isSucc)
            {
                //记日志并清缓存
                this.ClearEntityCache(userID);
            }
            return isSucc;
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        public DataTable GetList(string strWhere, int pageSize, int page, ref int allCount)
        {
            DataTable dt = ud.GetList(strWhere, pageSize, page, ref allCount);
            return dt;
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        public DataTable GetList(long departmentID)
        {
            DataTable dt = ud.GetList(departmentID);
            return dt;
        }

        /// <summary>
        /// 清除用户信息缓存
        /// </summary>
        private void ClearEntityCache(long userID)
        {
            string cacheName = "userdata_entity_" + userID;
            Cache.Remove(cacheName);
        }

        /// <summary>
        /// 获取加密密码
        /// </summary>
        /// <param name="originalPassword">原始密码</param>
        /// <returns>加密后的密码</returns>
        public string GetEncryptPassword(string originalPassword)
        {
            string encryptPassword = string.Empty;
            HashEncrypt ecy = new HashEncrypt(HashEncrypt.ServiceProviderEnum.MD5);
            encryptPassword = ecy.HashString(originalPassword);
            return encryptPassword;
        }
    }
}
